﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
using System.Web.UI;
using BusinessLogic.ClassFactory;
using DataAccess;
using Util.PageInfo;
using ValidateCommon;
using Common;

namespace RichECMS.ServerPages.Comment
{
    public partial class Update : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["Id"] != null)
                {
                    if (ParamAuth.IsNumber(Request["Id"].ToString().Trim()))
                    {
                        InitDataBind(Convert.ToInt32(Request["Id"].ToString()));
                        LitPaagePath.Text = "文章评论查看";
                        LitPageName.Text = "文章评论查看";
                    }
                    else
                    {
                        Alert("参数错误");
                    }
                }
                else
                {
                    Alert("参数错误");
                }
            }
        }

        /// <summary>
        /// 展示数据
        /// </summary>
        /// <param name="intCommentId">文章ID</param>
        private void InitDataBind(int intCommentId)
        {
            try
            {
                using (CommentFactory objFactory = new CommentFactory())
                {
                    BusinessLogic.BusinessRules.Comment objComment = objFactory.Creator(intCommentId);
                    if (objComment != null)
                    {
                        this.txtSummary.Value = objComment.DataItem.chvSummary;
                        this.txtContent.Value = objComment.DataItem.chvContent;
                        this.txtIssueTime.Value = objComment.DataItem.dtmInTime.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
                        this.txtTitle.Value = objComment.DataItem.chvTitle;
                        this.txtAuthor.Value = objComment.DataItem.chvAuthor;
                        this.txtOrder.Value = objComment.DataItem.intSort.ToString(CultureInfo.InvariantCulture);
                        if (objComment.DataItem.intVerify == 1)
                        {
                            CkboxPublish.Checked = true;
                        }
                        this.txtIP.Value = objComment.DataItem.chvIP;
                    }
                    else
                    {
                        Alert("该评论不存在");
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        /// <summary>
        /// 验证输入
        /// </summary>
        private bool ValidateValues()
        {
            bool bolIsValidate = true;
            if (this.txtTitle.Value.Trim().Length == 0)
            {
                Alert("请填写标题");
                bolIsValidate = false;
            }
            if (!ParamAuth.IsDateTime(SqlCommon.SqlStringFilter(this.txtIssueTime.Value)))
            {
                Alert("请输入正确的发布时间格式");
                bolIsValidate = false;
            }

            if (!ParamAuth.IsNumber(SqlCommon.SqlStringFilter(this.txtOrder.Value)))
            {
                Alert("排序请输入数字");
                bolIsValidate = false;
            }
            return bolIsValidate;
        }

        /// <summary>
        /// 提交数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SubmitBtnHander(object sender, ImageClickEventArgs e)
        {
            if (!ValidateValues())
            {
                return;
            }
            using (DBManager objDBManager = new DBManager())
            {
                objDBManager.BeginTransaction();
                try
                {

                    CommentFactory objFactory = new CommentFactory(objDBManager);
                    BusinessLogic.BusinessRules.Comment objComment = objFactory.Creator(Convert.ToInt32(Request["Id"].ToString()));
                    //插入评论表
                    objComment.DataItem.chvSummary = this.txtSummary.Value;
                    objComment.DataItem.chvContent = this.txtContent.Value;
                    objComment.DataItem.dtmInTime = Convert.ToDateTime(this.txtIssueTime.Value);
                    objComment.DataItem.chvTitle = this.txtTitle.Value;
                    objComment.DataItem.chvAuthor = State.LoginUser.LoginName;
                    objComment.DataItem.intSort = int.Parse(this.txtOrder.Value);

                    objComment.DataItem.intVerify = CkboxPublish.Checked == true ? 1 : 0;

                    //objComment.DataItem.chvIP = "127.0.0.1";
                    objComment.Update();

                    //提交
                    objDBManager.Commit();
                    objFactory.Dispose();
                }
                catch (Exception ex)
                {
                    Alert(ex.Message.ToString().Replace("\r\n", " "));
                    objDBManager.Rollback();
                    return;
                }
            }
            AlertAndRedirect("修改成功！", "List.aspx" , false);
        }
    }
}